﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using java = biz.ritter.javapi;
using biz.ritter.javapi.io;
using biz.ritter.javapi.lang;

namespace biz.ritter.javapi.util
{

    /**
     * Number-to-hexadecimal and hexadecimal-to-number conversions.
     *
     * @see <a href="http://johannburkard.de/software/uuid/">UUID</a>
     * @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
     * @version $Id: Hex.java 1888 2009-03-15 12:43:24Z johann $
     */

    public sealed class Hex
    {

        /**
         * No instances needed.
         */
        private Hex()
            : base()
        {
        }

        private static readonly char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

        /**
         * Turns a <code>short</code> into hex octets.
         *
         * @param a the {@link Appendable}, may not be <code>null</code>
         * @param in the integer
         * @return {@link Appendable}
         */
        public static Appendable append(Appendable a, short input)
        {
            return append(a, (long)input, 4);
        }

        /**
         * Turns a <code>short</code> into hex octets.
         *
         * @param a the {@link Appendable}, may not be <code>null</code>
         * @param in the integer
         * @param length the number of octets to produce
         * @return {@link Appendable}
         */
        public static Appendable append(Appendable a, short input, int length)
        {
            return append(a, (long)input, length);
        }

        /**
         * Turns an <code>int</code> into hex octets.
         *
         * @param a the {@link Appendable}, may not be <code>null</code>
         * @param in the integer
         * @return {@link Appendable}
         */
        public static Appendable append(Appendable a, int input)
        {
            return append(a, (long)input, 8);
        }

        /**
         * Turns an <code>int</code> into hex octets.
         *
         * @param a the {@link Appendable}, may not be <code>null</code>
         * @param in the integer
         * @param length the number of octets to produce
         * @return {@link Appendable}
         */
        public static Appendable append(Appendable a, int input, int length)
        {
            return append(a, (long)input, length);
        }

        /**
         * Turns a <code>long</code> into hex octets.
         *
         * @param a the {@link Appendable}, may not be <code>null</code>
         * @param in the long
         * @return {@link Appendable}
         */
        public static Appendable append(Appendable a, long input)
        {
            return append(a, input, 16);
        }

        /**
         * Turns a <code>long</code> into hex octets.
         *
         * @param a the {@link Appendable}, may not be <code>null</code>
         * @param in the long
         * @param length the number of octets to produce
         * @return {@link Appendable}
         */
        public static Appendable append(Appendable a, long input, int length)
        {
            try
            {
                int lim = (length << 2) - 4;
                while (lim >= 0)
                {
                    a.append(DIGITS[(byte)(input >> lim) & 0x0f]);
                    lim -= 4;
                }
            }
            catch (IOException ex)
            {
                // Bla
            }
            return a;
        }

        /**
         * Turns a <code>byte</code> array into hex octets.
         *
         * @param a the {@link Appendable}, may not be <code>null</code>
         * @param bytes the <code>byte</code> array
         * @return {@link Appendable}
         */
        public static Appendable append(Appendable a, byte[] bytes)
        {
            try
            {
                foreach (byte b in bytes)
                {
                    a.append(DIGITS[(byte)((b & 0xF0) >> 4)]);
                    a.append(DIGITS[(byte)(b & 0x0F)]);
                }
            }
            catch (IOException ex)
            {
                // Bla
            }
            return a;
        }

        /**
         * Parses a <code>long</code> from a hex encoded number. This method will skip all characters that are not 0-9,
         * A-F and a-f.
         * <p>
         * Returns 0 if the {@link CharSequence} does not contain any interesting characters.
         *
         * @param s the {@link CharSequence} to extract a <code>long</code> from, may not be <code>null</code>
         * @return a <code>long</code>
         * @throws NullPointerException if the {@link CharSequence} is <code>null</code>
         */
        public static long parseLong(CharSequence s)
        {
            long output = 0;
            byte shifts = 0;
            char c;
            for (int i = 0; i < s.length() && shifts < 16; i++)
            {
                c = s.charAt(i);
                if ((c > 47) && (c < 58))
                {
                    ++shifts;
                    output <<= 4;
                    output |= (long)c - 48;
                }
                else if ((c > 64) && (c < 71))
                {
                    ++shifts;
                    output <<= 4;
                    output |= (long)c - 55;
                }
                else if ((c > 96) && (c < 103))
                {
                    ++shifts;
                    output <<= 4;
                    output |= (long)c - 87;
                }
            }
            return output;
        }

        /**
         * Parses a <code>short</code> from a hex encoded number. This method will skip all characters that are not 0-9,
         * A-F and a-f.
         * <p>
         * Returns 0 if the {@link CharSequence} does not contain any interesting characters.
         *
         * @param s the {@link CharSequence} to extract a <code>short</code> from, may not be <code>null</code>
         * @return a <code>short</code>
         * @throws NullPointerException if the {@link CharSequence} is <code>null</code>
         */
        public static short parseShort(String s)
        {
            short output = 0;
            byte shifts = 0;
            char c;
            for (int i = 0; i < s.length() && shifts < 4; i++)
            {
                c = s.charAt(i);
                if ((c > 47) && (c < 58))
                {
                    ++shifts;
                    output <<= 4;
                    output |= (short)((short)c - 48);
                }
                else if ((c > 64) && (c < 71))
                {
                    ++shifts;
                    output <<= 4;
                    output |= (short)((short)c - 55);
                }
                else if ((c > 96) && (c < 103))
                {
                    ++shifts;
                    output <<= 4;
                    output |= (short)((short)c - 87);
                }
            }
            return output;
        }
    }
}
